package com.we.wecc.aw.mid.impl;

import com.we.wecc.aw.db.mapper.AwardCommentMapper;
import com.we.wecc.aw.db.mapper.AwardMapper;
import com.we.wecc.aw.db.model.Award;
import com.we.wecc.aw.db.model.AwardComment;
import com.we.wecc.aw.db.model.AwardCommentExample;
import com.we.wecc.aw.mid.model.in.AwardCommentIn;
import com.we.wecc.aw.mid.model.out.AwardCommentOut;
import com.we.wecc.aw.mid.service.AwardCommentsService;
import com.we.wecc.us.db.mapper.UserMapper;
import com.we.wecc.us.db.model.User;
import com.x.share.db.model.Pagination;
import com.x.share.mid.exception.HelperException;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AwardCommentsServiceImpl implements AwardCommentsService {

    @Resource
    AwardCommentMapper awardCommentMapper;

    @Resource
    AwardMapper awardMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public AwardComment saveAwardComment(AwardCommentIn in) {
        AwardComment ac = in.toAwardComments();
        Award award = awardMapper.selectByPrimaryKey(ac.getAwardId());
        if(award==null){
            throw new HelperException("该活动不存在！");
        }
        ac.setCreateTime(new Date());
        if(ac.getId()!=null && ac.getId()>0){
            AwardComment awardComment = awardCommentMapper.selectByPrimaryKey(ac.getId());
            if(awardComment==null){
                throw new HelperException("该评论不存在！");
            }
            awardCommentMapper.updateByPrimaryKeySelective(ac);
        }else{
            awardCommentMapper.insertSelective(ac);
        }
        return ac;
    }

    @Override
    public Boolean deleteAwardComment(int id) {
        Boolean flag = false;
        AwardComment ua = awardCommentMapper.selectByPrimaryKey(id);
        if(ua==null){
            throw new HelperException("该评论不存在！");
        }
        ua.setDelFlag(-1);
        int i = awardCommentMapper.updateByPrimaryKeySelective(ua);
        if(i == 1){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean agreeAwardComment(int id) {
        Boolean flag = false;
        AwardComment ua = awardCommentMapper.selectByPrimaryKey(id);
        if(ua==null){
            throw new HelperException("该评论不存在！");
        }
        ua.setAgreeNum(ua.getAgreeNum()+1);
        int i = awardCommentMapper.updateByPrimaryKeySelective(ua);
        if(i == 1){
            flag = true;
        }
        return flag;
    }

    @Override
    public Pagination<AwardCommentOut> getAwardCommentList(int awardId, Integer pageNo, Integer pageSize) {
        if(awardId <= 0){
            throw new HelperException("该活动不存在！");
        }
        AwardCommentExample ex = new AwardCommentExample();
        ex.or().andAwardIdEqualTo(awardId).andDelFlagEqualTo(0);
        ex.setOrderByClause("createTime desc");
        int count = (int)awardCommentMapper.countByExample(ex);
        Pagination<AwardCommentOut> page = new Pagination<>(pageNo, pageSize);
        RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
        List<AwardCommentOut> listAwardComment = new ArrayList<>();
        List<AwardComment> list = awardCommentMapper.selectByExampleWithRowbounds(ex,rb);
        list.forEach(s -> {
            Long dateTimeStamp = s.getCreateTime().getTime();
            User user = userMapper.selectByPrimaryKey(s.getUserId());
            AwardCommentOut awardCommentOut = new AwardCommentOut();
            awardCommentOut.setCommentTime(getCommentTime(dateTimeStamp));
            BeanUtils.copyProperties(user, awardCommentOut);
            ex.clear();
            ex.or().andParentIdEqualTo(s.getId());
            int commentNum = (int)awardCommentMapper.countByExample(ex);
            awardCommentOut.setCommentNum(commentNum);
            if(s.getParentId()>0){
                AwardComment ac = awardCommentMapper.selectByPrimaryKey(s.getParentId());
                if(ac!=null){
                    User u = userMapper.selectByPrimaryKey(ac.getUserId());
                    if(u!=null){
                        awardCommentOut.setParentName(u.getName());
                    }
                }
            }
            BeanUtils.copyProperties(s, awardCommentOut);
            listAwardComment.add(awardCommentOut);
        });
        page.setItems(listAwardComment);
        page.setRecords(count);
        return page;
    }

    @Override
    public Pagination<AwardCommentOut> getAwardCommentListByParentId(int awardId, int parentId, Integer pageNo, Integer pageSize) {
        if(awardId <= 0){
            throw new HelperException("该活动不存在！");
        }
        AwardCommentExample ex = new AwardCommentExample();
        ex.or().andAwardIdEqualTo(awardId).andDelFlagEqualTo(0).andParentIdEqualTo(parentId);
        ex.setOrderByClause("createTime desc");
        int count = (int)awardCommentMapper.countByExample(ex);
        Pagination<AwardCommentOut> page = new Pagination<>(pageNo, pageSize);
        RowBounds rb = new RowBounds(Pagination.offset(pageNo, pageSize), Pagination.limit(pageSize));
        List<AwardCommentOut> listAwardComment = new ArrayList<>();
        List<AwardComment> list = awardCommentMapper.selectByExampleWithRowbounds(ex,rb);
        list.forEach(s -> {
            Long dateTimeStamp = s.getCreateTime().getTime();
            User user = userMapper.selectByPrimaryKey(s.getUserId());
            AwardCommentOut awardCommentOut = new AwardCommentOut();
            awardCommentOut.setCommentTime(getCommentTime(dateTimeStamp));
            BeanUtils.copyProperties(user, awardCommentOut);
            ex.clear();
            ex.or().andParentIdEqualTo(s.getId());
            int commentNum = (int)awardCommentMapper.countByExample(ex);
            awardCommentOut.setCommentNum(commentNum);
            if(s.getParentId()>0){
                AwardComment ac = awardCommentMapper.selectByPrimaryKey(s.getParentId());
                if(ac!=null){
                    User u = userMapper.selectByPrimaryKey(ac.getUserId());
                    if(u!=null){
                        awardCommentOut.setParentName(u.getName());
                    }
                }
            }
            BeanUtils.copyProperties(s, awardCommentOut);
            listAwardComment.add(awardCommentOut);
        });
        page.setItems(listAwardComment);
        page.setRecords(count);
        return page;
    }

    private String getCommentTime(Long dateTimeStamp) {
        long minute = 1000 * 60;
        long hour = minute * 60;
        long day = hour * 24;
        long halfamonth = day * 15;
        long month = day * 30;
        long now = new Date().getTime();
        String result = "";
        long diffValue = now - dateTimeStamp;
        if(diffValue < 0) {
            throw new HelperException("该评论时间有异常！");
        }
        int monthC = (int) (diffValue/month);
        int weekC = (int) (diffValue/(7*day));
        int dayC = (int) (diffValue/day);
        int hourC = (int) (diffValue/hour);
        int minC = (int) (diffValue/minute);
        if(monthC>=1){
            if(monthC<=12)
                result="" + monthC + "月前";
            else{
                result="" + monthC/12 + "年前";
            }
        }
        else if(weekC>=1){
            result="" + weekC + "周前";
        }
        else if(dayC>=1){
            result=""+ dayC +"天前";
        }
        else if(hourC>=1){
            result=""+ hourC +"小时前";
        }
        else if(minC>=1){
            result=""+ minC +"分钟前";
        }else {
            result = "刚刚";
        }
        return result;
    }
}
